﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MusicRenamer;
using System.Globalization;
using System.Text.RegularExpressions;
using System.ComponentModel;

    public static class Helpers
    {
        public static readonly string[] musicExt = { "flac", "m4a", "mp3", "wma", "wav" };
        public static readonly string[] illegalChars = { "<", ">", ":", "\"", "/", "\\", "|", "?", "*" };

        /// <summary>
        /// Capitalizes any string
        /// </summary>
        /// <param name="s">String to capitalize</param>
        /// <returns>Capitalized string</returns>
        public static string Capitalize(this string s)
        {
            TextInfo USAText = new CultureInfo("en-US", false).TextInfo;
            return USAText.ToTitleCase(s.ToLower());
        }

        /// <summary>
        /// Checks if a string value matches an array of strings
        /// </summary>
        /// <param name="value">Value to be checked</param>
        /// <param name="contains">Array of strings</param>
        /// <returns>True/False if string is contained in array of strings</returns>
        public static bool Contains(this string value, string[] contains)
        {
            foreach (string val in contains)
            {
                if (value.Contains(val))
                {
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Removes illegal characters and underscores from file name
        /// </summary>
        /// <param name="s">String to check</param>
        /// <param name="removeText">Boolean to remove text</param>
        /// <param name="textToRemove">Text to remove from string</param>
        /// <returns>Properly formatted string</returns>
        public static string FormatTitle(this string s, bool removeText, string textToRemove)
        {
            //remove text before starting formatting
            if (removeText)
            {
                s = s.Replace(textToRemove, "");
            }

            string[] illegalChars = { "<", ">", ":", "\"", "/", "\\", "|", "?", "*" };
            foreach (string illegalChar in illegalChars)
            {
                s = s.Replace(illegalChar, "");
            }

            s = s.Replace("´", "'"); //replace weird single quotes
            s = s.Replace("_", " "); //replace underscores with spaces
            s = s.Trim();

            if (s.EndsWith("."))
            {
                s = s.Remove(s.LastIndexOf("."));
            }

            return s.Capitalize();
        }

        /// <summary>
        /// Formats the music filename in the format 00 Track Name.ext
        /// </summary>
        /// <param name="fileName">Name of file</param>
        /// <param name="dir">Directory where file is located</param>
        /// <param name="ext">File extension</param>
        /// <returns>Mp3 Object</returns>
        public static Mp3 FormatMp3(this string fileName, string dir, string ext, bool useDisc)
        {
            //Match m = Regex.Match(fileName, "(\\d?(\\d{1,2}))[- \\.]*(.*)");//"00 Track Name" format
            string trackNo = null,
                trackTitle = null;
            int discNo = 0;

            Match tripleDigit = Regex.Match(fileName, "(\\d)(\\d{2})[- \\.]*(.*)");
            Match singleDouble = Regex.Match(fileName, "(\\d{1,2})[- \\.]*(.+)");

            if (tripleDigit.Success && useDisc)
            {
                //1 = disc number
                //2 = track number
                //3 = track title
                Int32.TryParse(tripleDigit.Groups[1].ToString(), out discNo);
                trackNo = tripleDigit.Groups[2].ToString();
                trackTitle = tripleDigit.Groups[3].ToString().ToLower();
            }

            else if (singleDouble.Success)
            { 
                //1 - track no
                //2 - track title
                trackNo = singleDouble.Groups[1].ToString();
                trackTitle = singleDouble.Groups[2].ToString().ToLower();

                if (trackNo.Length == 1)
                {
                    trackNo = "0" + trackNo;
                }
            }


            if (singleDouble.Success || tripleDigit.Success)
            {
                if (trackTitle.Contains(ext))
                {
                    trackTitle = trackTitle.Replace("." + ext, "");
                }

                trackTitle = trackTitle.Trim();
                trackTitle = trackTitle.Capitalize();

                return new Mp3
                {
                    Title = trackTitle,
                    TrackNumber = trackNo,
                    Extension = ext,
                    Directory = dir,
                    DiscNumber = discNo
                };
            }

            return null;
        }
    }

    public class Mp3
    {
        public string Extension { get; set; }
        public string Title { get; set; }
        public string TrackNumber { get; set; }
        public string Directory { get; set; }
        public int DiscNumber { get; set; }
    }

    public class MusicInfo
    {
        public string FileName { get; set; }
        public string FileExtension { get; set; }
        public string Directory { get; set; }
        public BackgroundWorker Worker { get; set; }
        public string[] FileList { get; set; }
        public int FileIndex { get; set; }
        public TagLib.File mp3 { get; set; }
    }
